<?php

/*  PHP Paypal IPN Integration Class Demonstration File
 *  4.16.2005 - Micah Carrick, email@micahcarrick.com
 *
 *  This file demonstrates the usage of paypal.class.php, a class designed  
 *  to aid in the interfacing between your website, paypal, and the instant
 *  payment notification (IPN) interface.  This single file serves as 4 
 *  virtual pages depending on the "action" varialble passed in the URL. It's
 *  the processing page which processes form data being submitted to paypal, it
 *  is the page paypal returns a user to upon success, it's the page paypal
 *  returns a user to upon canceling an order, and finally, it's the page that
 *  handles the IPN request from Paypal.
 *
 *  I tried to comment this file, aswell as the acutall class file, as well as
 *  I possibly could.  Please email me with questions, comments, and suggestions.
 *  See the header of paypal.class.php for additional resources and information.
*/

// Setup class
$root_path="";

require("required/dbconnect.php");
require("required/email.php");
require("required/share_function.php");
require_once('paypal.class.php');  // include the class file
$get_info=mysql_query("select * from os_contactinfo where id=1");
$i=mysql_fetch_array($get_info,MYSQL_ASSOC);

$paypal_submit_url=$i['paypal_submit_url'];

 
$p = new paypal_class;             // initiate an instance of the class
$p->paypal_url = $paypal_submit_url;   // testing paypal url



if ($p->validate_ipn()) {
	
   // Payment has been recieved and IPN is verified.  This is where you
   // update your database to activate or process the order, or setup
   // the database with the user's order details, email an administrator,
   // etc.  You can access a slew of information via the ipn_data() array.

   // Check the paypal documentation for specifics on what information
   // is available in the IPN POST variables.  Basically, all the POST vars
   // which paypal sends, which we send back for validation, are now stored
   // in the ipn_data() array.

   // For this example, we'll just email ourselves ALL the data.
   
   $invoice_no=$p->ipn_data['invoice'];
   $payer_email=$p->ipn_data['payer_email'];
   $mc_gross=$p->ipn_data['mc_gross'];
   $txn_id=$p->ipn_data['txn_id'];
   $ipn_track_id=$p->ipn_data['ipn_track_id'];
   $payer_id=$p->ipn_data['payer_id'];
   
   
   
   
   $get_bid=mysql_query("select id from os_bill_details where invoice_number='$invoice_no'");
   
   if(mysql_num_rows($get_bid))
   {
	   $bid=mysql_result($get_bid,0,"id");
   }
   else
   {
   	   $bid="0";
   }
   
   mysql_query("insert into os_payment_record(bid,invoice_no,paymentdate,paymentbank,payer_paypal_email,payer_id,mc_gross,txn_id,ipn_track_id,ipn_values,create_datetime,is_paypal)values
   ('$bid','$invoice_no',now(),'paypal','$payer_email','$payer_id','$mc_gross','$txn_id','$ipn_track_id','$body',now(),1)");
   
   mysql_query("update os_bill_details set status='-4' where id='$bid'")or die(mysql_error());
   
  $get_items=mysql_query("select a.id,a.item_id,a.quantity,b.pid from os_bill_items a inner join os_product_inventory b on a.item_id = b.id where a.bid=$bid");
				
  while($i=mysql_fetch_array($get_items,MYSQL_ASSOC))
  {
	  $item_id=$i['item_id'];
	  $total_deduct=$i['quantity'];
	  $pid=$i['pid'];
	  updateInventory($item_id,$total_deduct,$bid,$pid);
  }
   
   email_paypal_payment_received($invoice_no);
  
}else{
	mysql_query("insert into os_payment_record(ipn_values,create_datetime)values('invalid IPN <br> $body',now())")or die(mysql_error());
}



?>